package com.zzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.domain.Attribute;
import com.zzx.domain.GoodsAttr;
import com.zzx.domain.Vo.GoodsAttrVo;
import com.zzx.mapper.GoodsAttrMapper;
import com.zzx.service.AttributeService;
import com.zzx.service.GoodsAttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品-属性关联表(GoodsAttr)表服务实现类
 *
 * @author makejava
 * @since 2022-08-02 14:06:44
 */
@Service("goodsAttrService")
public class GoodsAttrServiceImpl extends ServiceImpl<GoodsAttrMapper, GoodsAttr> implements GoodsAttrService {

    @Autowired
    private AttributeService attributeService;

    /**
     *获取商品参数
     * @param
     * @return
     */
    @Override
    public List<GoodsAttrVo> getGoodsAttrByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsAttr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsAttr::getGoodsId,goodsId);
        List<GoodsAttr> list = list(wrapper);
        List<GoodsAttrVo> collect = list.stream().map(item -> {
            Attribute byId = attributeService.getById(item.getAttrId());
            return  new GoodsAttrVo(item.getGoodsId(), item.getAttrId(), item.getAttrValue(), item.getAddPrice(), byId.getAttrName(), byId.getAttrSel(), byId.getAttrWrite(), byId.getAttrVals());
        }).collect(Collectors.toList());
        return collect;
    }
}

